package com.kede.api.stand.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.kede.exception.BadRequestException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/***
 ** @Author:Pr.li
 ** @Description:
 ** @Date: create in 2023/6/1 12:05
 ***/
@Slf4j
public class GaoDeMapUtil {

    private static String URL = "https://restapi.amap.com/v3/";

    private static String KEY = "8363d5de0bd5a2e6b10cccdead0c47b0";


    public static AddressComponentDto getAddressComponentByLocation(String lng,String lat){

        StringBuilder url = new StringBuilder();
        url.append(URL);
        url.append("geocode/regeo?");
        url.append("key=");
        url.append(KEY);
        url.append("&location=");
        url.append(lng);
        url.append(",");
        url.append(lat);

        String s = HttpUtil.get(url.toString());
        log.debug("获取逆地理编码url==={}",url.toString());
        log.debug("获取逆地理编码结果==={}",s);
        JSONObject jsonObject = JSONObject.parseObject(s);
        if (jsonObject.get("status").equals("1")) {
            JSONObject regeocode = JSONObject.parseObject(jsonObject.get("regeocode").toString());
            AddressComponentDto addressComponent = JSONObject.parseObject(regeocode.get("addressComponent").toString(), AddressComponentDto.class);
            return addressComponent;
        }else{
            throw new BadRequestException("获取逆地理编码出错");
        }
    }

    @Data
    public static class AddressComponentDto{
        String city;

        Integer citycode;

        String province;

        Integer adcode;

        String district;

        String township;
    }


    /**
     * 默认地球半径
     */
    private static double EARTH_RADIUS = 6371000;//赤道半径(单位m)

    /**
     * 转化为弧度(rad)
     * */
    private static double rad(double d)
    {
        return d * Math.PI / 180.0;
    }
    /**
     * @param lon1 第一点的精度
     * @param lat1 第一点的纬度
     * @param lon2 第二点的精度
     * @param lat2 第二点的纬度
     * @return 返回的距离，单位km
     * */
    public static double GetDistance(double lon1,double lat1,double lon2, double lat2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        s /=1000;
        return s;
    }

    public static double GetDistance(String lon1,String lat1,String lon2, String lat2){
        return GetDistance(Double.valueOf(lon1),Double.valueOf(lat1),Double.valueOf(lon2),Double.valueOf(lat2));
    }

    //    单位m
    private static double distanceUnitM(double centerLon, double centerLat, double targetLon, double targetLat) {

        double jl_jd = 102834.74258026089786013677476285;// 每经度单位米;
        double jl_wd = 111712.69150641055729984301412873;// 每纬度单位米;
        double b = Math.abs((centerLat - targetLat) * jl_jd);
        double a = Math.abs((centerLon - targetLon) * jl_wd);
        return Math.sqrt((a * a + b * b));

    }

    public static double distanceUnitM(String lon1,String lat1,String lon2, String lat2){
        return distanceUnitM(Double.valueOf(lon1),Double.valueOf(lat1),Double.valueOf(lon2),Double.valueOf(lat2));
    }

    public static void main(String[] args) {
        double v = distanceUnitM(113.13376, 40.99391, 113.13201, 40.99486);
        System.out.println("v = " + v);
    }

}
